package server.util;

import org.apache.commons.beanutils.Converter;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class DateTimeConverter implements Converter {
    private static final String DATE = "yyyy-MM-dd";
    private static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
    private static final String TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SSS";

    /* (non-Javadoc)
     * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
     */
    public <T> T convert(Class<T> type, Object value) {
        // TODO Auto-generated method stub
        return toDate(type, value);
    }

    private static <T> T toDate(Class<T> type, Object value) {
        if (value == null || "".equals(value)) {
            return null;
        }
        if (value instanceof String) {
            String dateValue = value.toString().trim();

            int length = dateValue.length();

            if (type.equals(java.util.Date.class)) {
                try {
                    DateFormat formatter = null;
                    if (length <= 10) {
                        formatter = new SimpleDateFormat(DATE, new DateFormatSymbols(Locale.CHINA));
                        return (T) formatter.parse(dateValue);
                    }
                    if (length <= 19) {
                        formatter = new SimpleDateFormat(DATETIME, new DateFormatSymbols(Locale.CHINA));
                        return (T) formatter.parse(dateValue);
                    }
                    if (length <= 23) {
                        formatter = new SimpleDateFormat(TIMESTAMP, new DateFormatSymbols(Locale.CHINA));
                        return (T) formatter.parse(dateValue);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return (T) value;
    }
}

